set more off
capture log close
global dir "directory"
log using "$dir/acamort/code/05_MJW_Bootstrap.log", replace
/*******************************************************************************
PROGRAM:	07_MJW_Bootstrap
PURPOSE:    This program implements the parametric bootstrap procedure
			outlined in Andrew Goodman-Bacon
DATE:		April 13, 2020
LAST UPDATED: 	November 12, 2020
********************************************************************************/
global temp "$dir/local/tempdata"


***A) Calculate confidence intervals for implied TOT estimates
capture program drop nhis
program define nhis
if `1'==1{
forval x=1/10000{
	*di "Loop `x'"
	clear
	qui: set obs 3437
	gen dum=runiform()
	gen nhis=dum<=.686
	collapse nhis
	gen ob=`x'
	qui: merge 1:1 ob using "$temp/temp", nogen
	qui: save "$temp/temp", replace
}
}
if `1'==0{
gen nhis=1
qui: save "$temp/temp", replace
}
end

capture program drop povmort
program define povmort
if `1'==1{
forval x=1/10000{
*di "Loop `x'"
	clear
	qui: set obs 73170
	gen dum=runiform()
	gen povmort=dum<=.0057925
	collapse povmort
	gen ob=`x'
	qui: merge 1:1 ob using "$temp/temp", nogen
	qui: save "$temp/temp", replace
}
}
if `1'==0{
gen povmort=1
qui: save "$temp/temp", replace
}
end

capture program drop allmort
program define allmort
if `1'==1{
forval x=1/10000{
*di "Loop `x'"
	clear
	qui: set obs 309045
	gen dum=runiform()
	gen allmort=dum<=0.0036182
	collapse allmort
	gen ob=`x'
	qui: merge 1:1 ob using "$temp/temp", nogen
	qui: save "$temp/temp", replace
}
}
if `1'==0{
gen allmort=1
qui: save "$temp/temp", replace
}
end

capture program drop ci
program define ci
clear
qui: set obs 10000
set seed `1'
gen mort=rnormal(`2', `3')
if `8'==1{
	gen med=rnormal(`4',`5')
}
gen ins=rnormal(`6',`7')
gen ob=_n
qui: save "$temp/temp", replace
nhis `9'
povmort `11'
allmort `11'
if `8'==1{
	gen pmed=((mort/(med/nhis))/(`10'*(povmort/allmort)))*100
	qui: su pmed
	local mean=r(mean)
	qui: su pmed if pmed<`mean', d
	local lower=r(p5)
	qui: su pmed if pmed>`mean', d
	local upper=r(p95)
	di "Medicaid CI=(`lower',`upper')"
}
gen punins=((mort/ins)/(`10'*(povmort/allmort)))*100
qui: su punins
local mean=r(mean)
qui: su punins if punins<`mean', d
local lower=r(p5)
qui: su punins if punins>`mean', d
local upper=r(p95)
di "Insured CI=(`lower',`upper')"
end

*program inputs= 1) seed (fr random number generator), 2) mort b, 3) mort se, 4) med b,
*5) med se, 6)ins b, 7) ins se, 8) med first stage avail, 9) med first stage survey-based
*10) baseline mortality, 11) adj for pov needed (1=yes, 0=no)

di "Miller, Johnson, Wherry 2020 Ages 55-64"
ci 1418390 -132 50 0.128 0.020 0.044 0.010 1 0 1630 0

di "Miller, Johnson, Wherry 2020 Ages 55-64 - different baseline"
ci 8990236 -132 50 0.128 0.020 0.044 0.010 1 0 2950 0

di "Miller, Johnson, Wherry 2020 Ages 55-64, continuous coverage"
ci 8663115 -132 50 0.375 0.061 0 0 1 0 1630 0

di "Miller, Johnson, Wherry 2020 Ages 55-64, continuous coverage - different baseline"
ci 1130324 -132 50 0.375 0.061 0 0 1 0 2950 0

di "Miller, Johnson, Wherry 2020 Placebo High-Income"
ci 5245240 -30 11 0.0208 0.00436 0 0 1 0 546 0

di "Miller, Johnson, Wherry 2020 Placebo High-Income, continuous coverage"
ci 7909915 -30 11 0.046 0.013 0 0 1 0 546 0

di "Miller, Johnson, Wherry 2020 Placebo High-Income, upper bound counterfactual"
ci 7168927 -30 11 0.0208 0.00436 0 0 1 0 3196 0

di "Miller, Johnson, Wherry 2020 Placebo High-Income, continuous coverage, upper bound"
ci 2637834 -30 11 0.046 0.013 0 0 1 0 3196 0

di "Miller, Johnson, Wherry 2020 Ages 19-64"
ci 8974029 -19 16.8 0.113 0.016 0.078 0.010 1 0 487 0

di "Black et al. 2019"
ci 3111637 -2.6 6.0 0 0 0.011 0.005 0 0 854 1

di "Borgschulte and Vogler, Ages 20-64"
ci 2626713 -11.4 3.6 0 0 0.042 0.011 0 0 359 1

di "Chen 2019 Ages 25-64"
ci 7851884 -8.3 4.9 0.191 0.004 0.140 0.004 1 1 386 1

di "Chen 2019 Ages 55-64"
ci 4682612 -51.3 18.5 0.191 0.008 0.114 0.007 1 1 859 1

di "Swaminathan et al. 2018"
ci 2757095 -600 204.1 0.105 0.014 0.042 0.009 1 0 6900 1

capture program drop ci2
program define ci2
clear
qui: set obs 10000
set seed `1'
gen mortins=rnormal(`2', `3')
gen ob=_n
qui: save "$temp/temp", replace
povmort 1
allmort 1
gen punins=(mortins/(`4'*(povmort/allmort)))*100
qui: su punins
local mean=r(mean)
qui: su punins if punins<`mean', d
local lower=r(p5)
qui: su punins if punins>`mean', d
local upper=r(p95)
di "Insured CI=(`lower',`upper')"
end

di "Sommers 2017"
ci2 8379286 -328.1 56.8 318

**add CI to level changes for response doc
capture program drop cilevel
program define cilevel
clear
qui: set obs 10000
set seed `1'
gen mort=rnormal(`2', `3')
gen med=rnormal(`4',`5')
gen ob=_n
qui: save "$temp/temp", replace

gen pmed=(mort/med)
qui: su pmed
local mean=r(mean)
qui: su pmed if pmed<`mean', d
local lower=r(p5)
qui: su pmed if pmed>`mean', d
local upper=r(p95)
di "Medicaid CI=(`lower',`upper')"

end
di "Miller, Johnson, Wherry 2020 Ages 55-64"
cilevel 1418390 -132 50 0.128 0.020

di "Miller, Johnson, Wherry 2020 Ages 55-64, continuous coverage"
cilevel 8663115 -132 50 0.375 0.061

di "Miller, Johnson, Wherry 2020 Placebo High-Income"
cilevel 5245240 -30 11 0.0208 0.00436

di "Miller, Johnson, Wherry 2020 Placebo High-Income, continuous coverage"
cilevel 7909915 -30 11 0.046 0.013


***B) Calculate confidence intervals for implied NNT estimates

capture program drop nntci
program define nntci
clear
qui: set obs 10000
set seed `1'
gen mort=rnormal(`2', `3')
gen med=rnormal(`4',`5')
gen ob=_n
qui: save "$temp/temp", replace
nhis `6'
gen nnt=1/(((-1*mort)/100000)/(med/nhis))
qui: su nnt
local mean=r(mean)
qui: su nnt if nnt<`mean', d
local lower=r(p5)
qui: su nnt if nnt>`mean', d
local upper=r(p95)
di "NNT estimate"
if `6'==1{
	di 1/(((-`2')/100000)/(`4'/.686))
}
if `6'==0{
	di 1/(((-`2')/100000)/`4')
}
di "NNT CI=(`lower',`upper')"
end

*program inputs= 1) seed (fr random number generator), 2) mort b, 3) mort se, 4) med b,
*5) med se, 6) correct for underreporting of survey-based Medicaid estimate

di "Miller, Johnson, Wherry 2020 Ages 55-64"
nntci 1418390 -132 50 0.128 0.020 0

di "Miller, Johnson, Wherry 2020 Ages 20-64"
nntci 8974029 -19 16.8 0.113 0.016 0

di "Chen 2019 Ages 25-64"
nntci 7851884 -8.3 4.9 0.191 0.004 1

di "Chen 2019 Ages 55-64"
nntci 4682612 -51.3 18.5 0.191 0.008 1

di "Swaminathan et al. 2018"
nntci 2757095 -600 204.1 0.105 0.014 0
